package servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Database.AuditLogDB;
import Database.StaffAppointmentDB;
import Logic.Authorizer;
import Model.Approval;
import Model.StaffAppointment;

/**
 * Servlet implementation class StaffappChangeStatus
 */
public class StaffappChangeStatus extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public StaffappChangeStatus() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		javax.servlet.http.HttpSession session = request.getSession();
		String approvedBy = (String) session.getAttribute("username");
		System.out.println(session.getAttribute("role"));
		if (session.getAttribute("username") == null) {
			request.getRequestDispatcher("login.jsp").forward(request,response);
			return;
		} else if (Authorizer.checkRole("2", session.getAttribute("role").toString()) || Authorizer.checkRole("3", session.getAttribute("role").toString())) {
				

			

	
		String md5;

		if (request.getParameterValues("check") != null) {

			String[] array = request.getParameterValues("check");
			for (int i = 0; i < array.length; i++) {
				md5 = array[i];

				Approval app = new Approval();
				app.setMd5(md5);
				app.setApprovedBy(approvedBy);

				if (request.getParameter("action").equals("Approve")
						|| request.getParameter("action").equals(
								"Approve & Print")) {
					// Approve
					app.setStatus("2");
					AuditLogDB.insertLog("approve appointment", (String) request
							.getSession().getAttribute("name"));
					StaffAppointmentDB.updateStatus(app);
					
				} else if (request.getParameter("action").equals("Remove")) {
					// Reject
					app.setStatus("3");
					AuditLogDB.insertLog("reject appointment", (String) request
							.getSession().getAttribute("name"));
					
					StaffAppointment so = new StaffAppointment();
					so.removeStaffappointment(md5);
					
				} else {
					// Delegate
					app.setStatus("4");
					AuditLogDB.insertLog("delegate appointment", (String) request
							.getSession().getAttribute("name"));
					StaffAppointmentDB.updateStatus(app);
				}

				
			}
			if (request.getParameter("action").equals("Approve")) {
				response.sendRedirect("staffapp_approve.jsp?error=2&action=approved");
			} else if (request.getParameter("action").equals("Approve & Print")) {
				request.setAttribute("row", array.length);
				for (int j = 0; j < array.length; j++) {
					request.setAttribute("md5" + j, array[j]);
				}
				request.getRequestDispatcher(
						"staffapp_approve.jsp?error=2&action=approved&print=true")
						.forward(request, response);
			} else if (request.getParameter("action").equals("Remove")) {
				response.sendRedirect("staffapp_approve.jsp?error=2&action=rejected");
			} else {
				response.sendRedirect("staffapp_approve.jsp?error=2&action=delegated");
			}
		} else {
			response.sendRedirect("staffapp_approve.jsp?error=1");
		}
			} else
				request.getRequestDispatcher("nopermission.jsp").forward(request,response);
				return;
	}

}
